<template>
  <div class="address-panel">
    <div class="address-title-box">
      <!-- 标题 -->
      <div class="address-title">配送至</div>
      <!-- 关闭按钮 -->
      <span class="address-close iconfont icon-guanbi" @click="emit('close')"></span>
    </div>
    <!-- 内容 -->
    <div class="address-content" v-if="userDeliveryAddrList.length > 0">
      <div class="address-content-item" v-for="(item, index) in userDeliveryAddrList" :key="index">
        <div class="item-box">
          <!-- 地址图标 -->
          <div class="address-location">
            <van-icon name="location" color="#27BA9B" size="24"/>
          </div>
          <div class="user-address">
            <div class="user">{{ item.name }} {{item.mobile}}</div>
            <div class="address">{{item.region}} {{ item.address }}</div>
          </div>
        </div>

        <!-- 选择按钮 -->
        <div>
          <span 
            @click="selectDeliveryAddr(item)" 
            class="select-address-icon iconfont" 
            :class="userStore.deliveryAddr?.id == item.id ? ' select-address-icon-checked icon-xuanzhong' : 'icon-weixuanzhong'"
          />
        </div>
      </div>
    </div>

    <!-- 空状态 -->
    <div class="blank" v-else>
      <image :src="blankImage" class="image" />
      <text class="text">您还没有添加收货地址哟！</text>
    </div>

    <div class="footer">
      <div class="button primary" @click="toDeliveryAddr"> 新建地址 </div>
      <div v-if="userDeliveryAddrList.length > 0" class="button primary" @click="emit('close')">确定</div>
    </div>
  </div>
</template>

<script setup>
import { reqUserDeliveryAddrListAPI } from "@/apis/userDeliveryAddr"
import { ref, onMounted } from 'vue'
import { useUserStore } from '@/store/modules/user'
import blankImage from '@/assets/images/blank.png'

// 用户存储
const userStore = useUserStore()

const emit =  defineEmits(['close'])




// ------------------------ 获取收货地址功能开始 ------------------------
const userDeliveryAddrList = ref([])
const getUserDeliveryAddrList = async () => {
  const result = await reqUserDeliveryAddrListAPI({status: 1})
  userDeliveryAddrList.value = result.data.user_delivery_addrs != null ? result.data.user_delivery_addrs : []
}
// ------------------------ 获取收货地址功能结束 ------------------------




// ------------------------ 选择收货地址功能开始 ------------------------
const selectDeliveryAddr = (item) => {
  userStore.setDeliveryAddr(item)
}
// ------------------------ 选择收货地址功能结束 ------------------------




// ------------------------ 跳转收货地址功能开始 ------------------------
const toDeliveryAddr = () => {
  uni.navigateTo({
    url: '/pagesDeliveryAddr/delivery_address/delivery_address',
  })

  emit('close')
}
// ------------------------ 跳转收货地址功能结束 ------------------------

onMounted(() => {
  getUserDeliveryAddrList()
})

</script>

<style scoped lang="scss">
.address-panel {
  display: flex;
  flex-direction: column;
  padding: 0 5px;
  background-color: #fff;
  .address-title-box {
    display: flex;
    justify-content: space-between;
    padding: 15px 5px;
    // 标题
    .address-title {
      font-weight: bold;
    }

    // 关闭
    .address-close {
      font-size: 22px;
      color: #27ba9b;
    }
  }

  // 主体
  .address-content {
    min-height: 150px;
    max-height: 370px;
    overflow: auto;
    padding-right: 5px;
    .address-content-item {
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      align-items: center;
      padding: 15px 10px;
      border-top: 1px solid gainsboro;
      .item-box {
        display: flex;
        flex-direction: row;
        align-items: center;
        .user-address {
          .user {
            font-size: 14px;
            color: #444;
            font-weight: bold;
          }
          .address {
            margin-top: 10px;
            font-size: 13px;
            color: #666;
          }
        }
      }

      .select-address-icon {
        color: #999;
        font-size: 24px;
      }
      .select-address-icon-checked {
        color: #27ba9b;
      }
    }
    .icon-ring {
      color: #444;
    }
  }

  // 尾部
  .footer {
    display: flex;
    justify-content: space-between;
    padding: 30px 0 20px;
    font-size: 14px;
    color: #444;

    .button {
      flex: 1;
      height: 36px;
      text-align: center;
      line-height: 36px;
      margin: 0 10px;
      color: #fff;
      border-radius: 36px;
    }

    .primary {
      color: #fff;
      background-color: #27ba9b;
    }

    .secondary {
      background-color: #ffa868;
    }
  }
}




// 空状态
.blank {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 30vh;
  .image {
    width: 200px;
    height: 141px;
  }
  .text {
    color: #444;
    font-size: 13px;
    margin: 10px 0;
  }
}
</style>